<script setup lang='ts'>
  import { onMounted, reactive, ref } from 'vue';
  import { ChallengeApi } from '@/api/controller'
  import { ToastSuccess, ToastWarning, Toastloading } from '@/utils/public';
  import updateForm from './updateForm.vue';
  import { ElMessageBox } from 'element-plus';
  const updateFormRef = ref()
  const action = reactive({
    gameList:[] as Array<any>,
    list:[] as Array<any>,
    copylist:[] as Array<any>
  })
  const listQuery = reactive({
    game_id:'',
  })
  const typeChange = (e:any)=>{
    if(!e) {
        listQuery.game_id = ''
        getList()
        return
      }
      const obj = action.gameList.find(res=> res.id == e)
      const { game_id, item_id, id } = obj;
      let data = action.copylist
      let list = data.filter(res=>{
        return game_id == res.game_id && item_id == res.item_id
      })
      action.list = list
  }
  const handleUpdate = (row:any = {})=>{
    updateFormRef.value.openDialog(row)
  }
  const handleDelete = (row:any = {})=>{
    const { id=0, item_name='' } = row
    if(!id) return ToastWarning('缺少id')
    ElMessageBox.confirm(`确认删除${item_name}技能的配置吗?`, "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    }).then(async () => {
      const loading = Toastloading();
      ChallengeApi.skillDelete({id}).then(()=>{
        ToastSuccess("删除成功!");
        loading.close();
        getList();
      }).catch(()=>{
        loading.close();
      })
    }).catch(() => {});
  }
  const getList = async()=>{
    const loading = Toastloading()
    const res = await ChallengeApi.skillList()
    const { data=[] } = res || {}
    action.list = data
    action.copylist = data
    let options = []
    for(let item of data) {
      const { game_id, game_name, item_id, item_name } = item;
      const index = options.findIndex(obj=> {
        return Number(`${game_id}${item_id}`) == obj.id
      })
      if(index == -1) options.push({
        id:Number(`${game_id}${item_id}`),
        game_id,
        name:`${game_name}-${item_name}`,
        game_name,
        item_id,
        item_name
      })
    }
    action.gameList = options
    loading.close()
  }
  onMounted(() => {
    getList()
  })
</script>

<template>
  <div class="xm-content">
    <div class="xm-headTab">
      <div class="labelBox">
        <div class="labelItem">
          <div class="label">类型：</div>
          <el-select v-model="listQuery.game_id" @change="typeChange" clearable placeholder="请选择类型">
            <el-option
              v-for="item in action.gameList"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </div>
        <div class="labelItem">
          <el-button type="primary" @click="typeChange(0)">刷新</el-button>
          <el-button type="primary" @click="handleUpdate()">新增</el-button>
        </div>
      </div>
    </div>
    <el-table
      class="xm-main xm-TableCenter"
      :data="action.list"
      border>
      <el-table-column min-width="120" prop="game_name" align="center" label="游戏类型" />
      <el-table-column min-width="120" prop="item_id" align="center" label="技能id" />
      <el-table-column min-width="120" prop="item_name" align="center" label="技能名称" />
      <el-table-column min-width="120" prop="rate" align="center" label="概率">
        <template #default="scope">{{ scope.row.rate }}%</template>
      </el-table-column>
      <el-table-column min-width="120" prop="draw_num" align="center" label="参与次数" />
      <el-table-column min-width="120" prop="skill_reward" align="center" label="人物id范围">
        <template #default="scope">{{ scope.row.skill_reward.toString() }}</template>
      </el-table-column>
      <el-table-column width="200" fixed=right label="操作">
        <template #default="scope">
          <el-button type="primary" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button v-if="scope.row.can_delete == 1" type="danger" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <updateForm ref="updateFormRef" @update-list="getList" />
  </div>
</template>

<style lang='scss' scoped>

</style>
